package com.yfp.machine.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.machine.domain.Ad;
import com.yfp.machine.domain.AdResource;
import com.yfp.machine.domain.dto.AdDto;
import com.yfp.machine.domain.vo.AdResourceVo;
import com.yfp.machine.mapper.AdMapper;
import com.yfp.machine.mapper.AdResourceMapper;
import com.yfp.machine.service.AdService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (Ad)表服务实现类
 *
 *
 * @since 2023-12-28 14:52:00
 */
@Service("adService")
public class AdServiceImpl implements AdService {
    @Resource
    private AdMapper adMapper;

    @Resource
    private AdResourceMapper adResourceMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param adId 主键
     * @return 实例对象
     */
    @Override
    public AdDto queryById(String adId) {
        Ad ad = new Ad();
        ad.setAdId(adId);
        List<AdDto> adDtoList = this.adMapper.queryAll(ad);
        if (CollUtil.isEmpty(adDtoList)) return null;
        AdDto adDto = CollUtil.getFirst(adDtoList);
        String url = this.adResourceMapper.queryAdImgOrVideoUrlByAdId(adDto.getAdId(), adDto.getType());
        if (CharSequenceUtil.isNotEmpty(url)) adDto.setResourceUrl(url);
        return adDto;
    }

    /**
     * 分页查询
     *
     * @param ad 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Ad> queryByPage(Ad ad) {
        return this.adMapper.queryAllByLimit(ad);
    }

    /**
     * 新增数据
     *
     * @param ad 实例对象
     * @return 实例对象
     */
    @Override
    public Ad insert(Ad ad) {
        this.adMapper.insert(ad);
        return ad;
    }

    /**
     * 修改数据
     *
     * @param ad 实例对象
     * @return 实例对象
     */
    @Override
    public AdDto update(Ad ad) {
        this.adMapper.update(ad);
        return this.queryById(ad.getAdId());
    }

    /**
     * 全部查询
     *
     * @param ad 广告
     * @return {@link List}<{@link Ad}>
     */
    @Override
    public List<AdDto> queryAdAll(Ad ad) {
        List<AdDto> ads = this.adMapper.queryAll(ad);
        return ads.stream().map(a -> {
            String url = this.adResourceMapper.queryAdImgOrVideoUrlByAdId(a.getAdId(), a.getType());
            if (CharSequenceUtil.isNotEmpty(url)) a.setResourceUrl(url);
            return a;
        }).toList();
    }

    /**
     * 新增广告资源
     *
     * @param adResourceVo 广告资源vo
     * @return int
     */
    @Override
    public boolean addAdResource(AdResourceVo adResourceVo) {
        adResourceNameVerifyDuplicate(adResourceVo);

        Snowflake snowflake = new Snowflake(1);
        String adId = String.valueOf(snowflake.nextId());

        Ad ad = new Ad();
        ad.setAdId(adId);
        ad.setName(adResourceVo.getName());
        ad.setType(adResourceVo.getResourceType());
        ad.setRemark(adResourceVo.getRemark());
        int insertAd = this.adMapper.insert(ad);

        AdResource adResource = new AdResource();
        adResource.setAdId(adId);
        adResource.setResourceId(adResourceVo.getResourceId());
        adResource.setResourceType(adResourceVo.getResourceType());
        adResource.setDuration(adResourceVo.getDuration());
        int insertAdRes = this.adResourceMapper.insert(adResource);

        return insertAd > 0 && insertAdRes > 0;
    }

    /**
     * AD resource name verification duplicate
     * AD resource name verification duplicate
     *
     * @param adResourceVo 广告资源vo
     */
    private void adResourceNameVerifyDuplicate(AdResourceVo adResourceVo) {
        int count = this.adMapper.countByAdName(adResourceVo.getName(), adResourceVo.getAdId());
        if (count > 0) throw new ServiceException("广告名称已存在");
    }

    /**
     * 更新广告资源
     *
     * @param adResourceVo 广告资源
     * @return boolean
     */
    @Override
    public boolean updateAdResourceVo(AdResourceVo adResourceVo) {
        if (CharSequenceUtil.isEmpty(adResourceVo.getAdId())) throw new ServiceException("广告id不能为空");
        adResourceNameVerifyDuplicate(adResourceVo);

        AdResource adResourceResult = this.adResourceMapper.queryById(adResourceVo.getAdId());
        if (!Objects.equals(adResourceVo.getResourceType(), adResourceResult.getResourceType())) {
            throw new ServiceException("资源类型不允许修改");
        }

        AdResource adResource = new AdResource();
        adResource.setAdId(adResourceVo.getAdId());
        adResource.setDuration(adResourceVo.getDuration());
        adResource.setResourceId(adResourceVo.getResourceId());
        adResource.setResourceType(adResourceVo.getResourceType());
        int updateAdRes = this.adResourceMapper.update(adResource);

        Ad ad = new Ad();
        ad.setRemark(adResourceVo.getRemark());
        ad.setUpdateTime(new Date());
        ad.setName(adResourceVo.getName());
        ad.setAdId(adResourceVo.getAdId());
        ad.setType(adResourceVo.getResourceType());
        ad.setUpdateTime(new Date());
        int updateAd = this.adMapper.update(ad);
        return updateAdRes > 0 && updateAd > 0;
    }

    /**
     * 删除广告
     *
     * @param adId 广告id
     * @return boolean
     */
    @Override
    public boolean deleteAd(String adId) {
        Ad ad = new Ad();
        ad.setAdId(adId);
        ad.setIsDelete(true);
        return this.adMapper.delAd(ad) > 0;
    }
}
